package com.situ.web.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.BanJi;
import com.situ.web.service.IBanJiCourseService;
import com.situ.web.service.impl.BanJiCourseServiceImpl;

import net.sf.json.JSONArray;

public class BanJiCourseServlet extends HttpServlet {
	private IBanJiCourseService banJiCourseService = new BanJiCourseServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "pageList":
			pageList(req,resp);
			break;
		case "selectBanJi":
			selectBanJi(req,resp);
			break;

		default:
			break;
		}
	}

	private void selectBanJi(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		List<BanJi> list = banJiCourseService.selectBanJi();
		resp.setContentType("text/html;charset=utf-8");
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.getWriter().write(jsonArray.toString());
	}

	private void pageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Map<String, Object>> list = banJiCourseService.findPageList();
		System.out.println(list);
		req.setAttribute("list", list);
		req.getRequestDispatcher("/banji_course_list.jsp").forward(req, resp);
		
	}

}
